package com.lsc.jdbc.demo.jdbc.utiltest;

import com.lsc.jdbc.demo.jdbc.util.JdbcUtils;
import com.lsc.jdbc.demo.jdbc.util.JdbcUtils_C3P0;

import java.sql.*;

public class JdbcUtilsTest_C3P0 {

    static Connection con;
    static Statement statement;
    static ResultSet resultSet;
    static PreparedStatement preparedStatement;

    /**
     * 链接数据库的  静态代码块
     */
    static {
        con = JdbcUtils_C3P0.getCon();
    }


    /**
     * 测试  数据库的工具类 是否可以正常使用
     */
    public static void query() {

        /* Statement 会出现 sql  注入  */
        try {
            statement = con.createStatement();
            String sql = "select * from stu_info";
            resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            System.out.println("stu_id\tstu_name\tstu_sex");
            while (resultSet.next()) {
                for (int i = 0; i < columnCount; i++) {
                    System.out.print(resultSet.getObject(i + 1) + "\t");
                }
                System.out.println();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils.release(con, statement, resultSet, null);
        }


    }


    public static void main(String[] args) {

        query();


    }


}
